//帖子对象
var post = new Vue({

    methods:{
        //所有帖子列表  //查询操作
        getPostInfo:function () {
            layui.use('table', function(){
                var table = layui.table;
                table.render({
                    elem: '#postInfo'
                    ,url: '/post/list'
                    ,cols: [[
                        {checkbox: true}
                        ,{field: 'title', title: '帖子标题', width:230}
                        ,{field: 'content', title: '帖子内容'}
                        ,{field: 'createtime', title: '创建时间', width:180}
                        ,{field: 'forumname', title: '所属版块', width:100}
                        ,{field: 'nickname', title: '帖主名称', width:120}
                        ,{field: 'status', title:'状态',  edit: 'text', width:100, sort: true}
                    ]]
                    ,id: 'postInfo'
                    ,height: 397
                });

                //查询操作
                var $ = layui.$, active = {
                    reload: function(){
                        var demoReload = $('#post_s').val();
                        if(demoReload != null){
                            //执行重载
                            table.reload('postInfo', {
                                elem: '#postInfo'
                                ,url: '/post/select?title='+demoReload //数据接口
                                ,cols: [[
                                {checkbox: true}
                                    ,{field: 'title', title: '帖子标题', width:230}
                                    ,{field: 'content', title: '帖子内容'}
                                    ,{field: 'createtime', title: '创建时间', width:180}
                                    ,{field: 'forumname', title: '所属版块', width:100}
                                    ,{field: 'nickname', title: '帖主名称', width:120}
                                    ,{field: 'status', title:'状态', edit: 'text', width:100, sort: true}
                            ]]
                                ,id: 'postInfo'
                                ,height: 397
                            });
                        }else{
                            table.reload('postInfo', {
                            });
                        }
                    }
                };
                $('#select').on('click', function(){
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
            });
        },

        //删除操作
        getPostHandel:function () {
            layui.use('table', function(){
                var table = layui.table;
                //监听表格复选框选择
                table.on('checkbox(postInfo)', function(obj){
                });

                var $ = layui.$, active = {
                    getCheckData: function(){
                        var checkStatus = table.checkStatus('postInfo')
                            ,data = checkStatus.data;
                        if(data.length != 0)
                        {
                            var if_delete = true;
                            $.each(data,function (index,obj) {
                                axios.delete('/post/delete?id='+obj.id)
                                    .then(function (response) {
                                        if(response !== null){
                                            if_delete = true;
                                        }else {
                                            if_delete = false;
                                        }
                                    })
                                    .catch(function (error) {
                                        console.log(error);
                                    });
                            });
                            if(if_delete === true){
                                layer.alert('删除成功！')
                            }else {
                                layer.alert('删除失败！')
                            }
                        }else {
                            layer.alert('请选择对象！')
                        }

                    }
                };

                $('#delete').on('click', function(){
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
            });
        },

        //编辑操作
        getPostUpdate:function () {
            layui.use('table', function(){
                var table = layui.table;

                //监听单元格编辑
                table.on('edit(postInfo)', function(obj){
                    var name = obj.value //得到修改后的值
                        ,id = obj.data.id //得到所在行所有键值
                        ,value = obj.field; //得到字段
                    $.ajax({
                        type:"POST",
                        url:"/post/update",
                        data:{"id":id, "status":name},
                        async:false,
                        success:function (data) {
                            if(data.code != null){
                                layer.alert('修改成功！')
                            }else {
                                layer.alert('修改失败！')
                            }

                        }
                    });
                });
            });

        }

    }
});